﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Services;
using System.Web.Mvc;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core;
using MvcEngine.Core.Registry;
using MvcEngine.Mvc.Filters;
using MvcEngine.Mvc;
using MvcEngine.Gallery.Commands;
using MvcEngine.Gallery.ModelBinders;
using MvcEngine.Gallery.Models;
using MvcEngine.Mvc.Services;


namespace MvcEngine.Gallery.Services
{
    public class GalleryControlService : ControlServiceBase
    {
        public override void RegisterBackofficeRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "BackOffice_GalleryList",
                "BackOffice/Galleries",
                new { controller = "Gallery", action = "List" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_GalleryInstall",
                "BackOffice/Module/Gallery/Install/{pageId}",
                new { controller = "Gallery", action = "Install" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_GallerySettings",
                "BackOffice/{galleryName}/Settings",
                new { controller = "Gallery", action = "Settings" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_GallerySettingsSave",
                "BackOffice/{galleryName}/Settings/Save",
                new { controller = "Gallery", action = "SettingsSave" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );
        }

        public override IList<ControlCommand> GetCommands()
        {
            var commands = new List<ControlCommand>();
            commands.Add(new GalleryInstallCommand() { Name = "Install", LocalizedName = "Install", Visible = false });
            commands.Add(new GalleryListCommand() { Name = "List", LocalizedName = "List", Visible = false });
            return commands;
        }

        public override void RegisterActionFilters(Core.Registry.IActionFilterRegistry registry)
        {
            //--------------------------------------------------------------------------------------
            //------------------------------Common filters------------------------------------------
            //--------------------------------------------------------------------------------------
            ControllerCriteria backOfficeCommonCriteria = new ControllerCriteria();
            backOfficeCommonCriteria.AddController(typeof(MvcEngine.Gallery.BackOffice.Controllers.GalleryController));
            registry.Add(new[] { backOfficeCommonCriteria }, typeof(MvcEngine.Gallery.BackOffice.Filters.GalleryActionFilter));

            //ControllerCriteria starterSiteCommonCriteria = new ControllerCriteria();
            //starterSiteCommonCriteria.AddController(typeof(MvcEngine.Blog.StarterSite.Controllers.BlogTagController));
            //registry.Add(new[] { starterSiteCommonCriteria }, typeof(MvcEngine.Blog.StarterSite.Filters.BlogActionFilter));

            ////--------------------------------------------------------------------------------------
            ////------------------------------End of common filters-----------------------------------
            ////--------------------------------------------------------------------------------------

            ControllerCriteria moduleCriteria = new ControllerCriteria();
            moduleCriteria.AddController(typeof(MvcEngine.Gallery.BackOffice.Controllers.GalleryController));
            registry.Add(new[] { moduleCriteria }, new ModuleActionFilter());




            ////--------------------------------------------------------------------------------------
            ////---------------------------------Permissions------------------------------------------
            ////--------------------------------------------------------------------------------------
            //ControllerCriteria viewCriteria = new ControllerCriteria();
            //viewCriteria.AddController(typeof(MvcEngine.Blog.StarterSite.Controllers.BlogTagController));

            //ControllerCriteria editCriteria = new ControllerCriteria();
            //editCriteria.AddController(typeof(MvcEngine.Blog.BackOffice.Controllers.BlogTagController));
            ////--------------------------------------------------------------------------------------
            ////---------------------------------End of permissions----------------------------------
            ////--------------------------------------------------------------------------------------

            //registry.Add(new[] { editCriteria }, new RolePermissionAttribute(Constants.Roles.Administrators));
            //registry.Add(new[] { viewCriteria }, new ModulePermissionAttribute(PermissionTypes.View));
        }

        public override void RegisterModelBinders(ModelBinderDictionary binders)
        {
            binders[typeof(GalleryData)] = new GalleryModelBinder();    
        }

        public override Type GetControllerType()
        {
            return null;
        }

        public override string GetDefaultAction()
        {
            return null;
        }
    }
}
